﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectDES
{
    public partial class Server : Form
    {
        private string cryptStr = "";
        private delegate void UpdateStatusCallback(string strMessage);
        public Server()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Server_FormClosing);
        }
        void Server_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            IPAddress ipAddr = IPAddress.Parse(txtIp.Text);
            ChatServer mainServer = new ChatServer(ipAddr);
            ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
            mainServer.StartListening();
            btnListen.Enabled = false;
            btnListen.Text = "Listening...";
            txtIp.Enabled = false;
            Name1.Start();
            MessageBox.Show("Note, Default Administrative Username and password is. Username: Admin, Password: 123987", "Default Admin Username/Pass");
        }
        public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }
        private void UpdateStatus(string strMessage)
        {
            if (rbTripDes.Checked && !cryptStr.Equals(""))
            {
                txtEncrypted.Clear();
                txtEncrypted.Text = cryptStr;
                //string decryptedText = TripleDESEngine.Decrypt(cryptStr, true);
                cryptStr = "";
                //txtLog.AppendText(decryptedText + "\r\n");
                txtLog.AppendText(strMessage + "\r\n");            
            }
            else if (rbDes.Checked && !cryptStr.Equals(""))
            {
                txtEncrypted.Clear();
                txtEncrypted.Text = cryptStr;
                //string decrypedText = DESEngine.Decrypt(cryptStr, true);
                cryptStr = "";
                //txtLog.AppendText(decrypedText + "\r\n");
                txtLog.AppendText(strMessage + "\r\n");
            }
            else
            {
                txtLog.AppendText(strMessage + "\r\n");
            }
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (rbTripDes.Checked && !String.IsNullOrEmpty(txtMsg.Text))
            {
                string clearText = txtMsg.Text.Trim();
                string cipherText = TripleDESEngine.Encrypt(clearText, true);
                ChatServer.SendAdminMessage(cipherText);
                cryptStr = cipherText;
                txtMsg.Clear();
            }
            if (rbDes.Checked&& !String.IsNullOrEmpty(txtMsg.Text))
            {
                string clearText = txtMsg.Text.Trim();
                string cipherText = DESEngine.Encrypt(clearText, false);
                ChatServer.SendAdminMessage(cipherText);
                cryptStr = cipherText;
                txtMsg.Clear();
            }
            else
            {
                ChatServer.SendAdminMessage(txtMsg.Text);
                txtMsg.Clear();
            }
            
        }

        private void txtMsg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (txtMsg.Text != "") 
                {
                    ChatServer.SendAdminMessage(txtMsg.Text);
                    txtMsg.Clear();
                }
                
            }
        }

        private void Server_Load(object sender, EventArgs e)
        {
            
        }
    }
}
